﻿define(function (require, exports, module) {
    seajs.config({
        charset: 'utf-8',
        timeout: 5000,
        debug: true
    });

    require("module/es5-safe")();
    require("module/modernizr")();

    exports._ = require("module/underscore");

    exports.JSON = require("module/json");
    exports.jsonHelp = require("./plus/jsonhelp");

    var $ = require('module/jquery');
    $.ajaxSetup({
        type: "POST",
        dataType: "json", //返回值类型
        contentType: "application/x-www-form-urlencoded; charset=utf-8", //请求参数类型
        timeout: 5000
    });
    require("./plus-jq/jq_bootstrap")($);
    require('./plus-jq/jq_initbow')($);
    require('./plus-jq/jq_overlabel')($);
    require('./plus-jq/jq_countable')($);
    require('./plus-jq/jq_cookie')($);
    require('./plus-jq/jq_mttext')($);
    require('./plus-jq/jq_propcount')($);
    require('./plus-jq/jq_serializehelp')($, exports.JSON);
    require('./plus-jq/jq_validation')($, exports._);
    require('./plus-jq/jq_selection')($);
    require('./plus-jq/jq_paginate')($);
    require('./plus-jq/ms/jq_validate')($);
    require('./plus-jq/ms/jq_validate_unobtrusive')($);
    require('./plus-jq/ms/jq_ajax_unobtrusive')($);
    //var target = require('./jqplus/jq_target');
    //target.Init($, "#head ,#main, #foot");
    var uihelp = require('./plus-jq/ui/jq_ui_help');
    uihelp.init($);
    exports.jq = $;

    exports.loader = function () {
        window.win = $(window);
        $("body").addClass(window.bow);
        //target.loader();
        exports.fixui($("body"));
        uihelp.Loader($);
    }

    exports.fixui = function (box, includ_me) {
        $('input.text, textarea.text', box).mttext();
        $("input.selall", box).click(function () { $(this).select() });
        $('.overlabel').find('label').overlabel();

        $(".countable", box).each(function () {
            $(this).countable({
                threshold: 0,
                target: $(this).parent().find(".cttarget"),
                appendMethod: "appendTo",
                positiveCopy: "还可输入{n}个字符"
            });
        });

        $("input[accesskey]", box).bind("keydown", function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                var btnid = $(this).attr('accesskey');
                $(".access_" + btnid).click();
            }
        });

        //module.load('./plus-jq/jq_maxlimit', function () {
        //    $("[class*='max-']", box).maxLimit({ size: [120, 160, 320, 600] });
        //});

        $('.overlabel').find('label').overlabel();

        $(".hov", box).hover(
			function () { $(this).addClass('hover'); },
			function () { $(this).removeClass('hover'); }
		);

        $('a.submit', box).click(function () {
            $(this).closest('form').submit();
        });
        $('input.submit', box).bind("keydown", function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code != 13) return;
            $(this).closest('form').submit();
        });
        $(".pager", box).each(function () {
            var o = $(this);
            if (o.attr('count') == 0) return;
            o.paginate({
                count: Math.ceil(parseInt(o.attr('count')) / parseInt(o.attr('size'))),
                start: parseInt(o.attr('index')),
                display: 12,
                border: false,
                text_color: '#79B5E3',
                background_color: 'none',
                text_hover_color: '#2573AF',
                background_hover_color: 'none',
                images: false,
                mouse: 'press',
                onChange: function (num) {
                    window.location.href = o.attr('url') + num;
                }
            });
        });
        $("select.change_url").bind("change", function () {
            var type = $(this).getSelectedValue();
            window.location.href = $(this).attr('url') + type;
        });

        $('img#ajaxload', box).ajaxStart(function () {
            $(this).show();
        });
        $('img#ajaxload', box).ajaxStop(function () {
            $(this).hide();
        });

        //target.fixui();
    }

    exports.dynamicwin = function () {
        var calculateLayout = function () {
            window.winHeight = window.win.height();
            window.winWidth = window.win.width();
        };
        calculateLayout();
        var lazyLayout = exports._.debounce(calculateLayout, 300);
        window.win.resize(lazyLayout);
    }

    exports.notifi = {};
    exports.shownotifi = function (txt, type, key) {
        var o = $('<div class="notifi"><span class="txt"></span><span class="close" title="关闭"></span></div>');
        if (key) {
            if (exports.notifi[key]) return;
            exports.notifi[key] = o;
        }
        o.addClass(type);
        $('span.txt', o).text(txt);
        $('span.close', o).click(function () { o.fadeOut("slow"); });
        $("#notifi-box").html(o);
    }
    exports.clearnotifi = function (key) {
        if (key) {
            if (!exports.notifi[key]) return;
            exports.notifi[key].remove();
            exports.notifi = exports._.without(exports.notifi, key);
        }
        else
            $("#notifi-box").empty();
    }

    exports.checkreturn = function (m) {
        if (m.result == false) {
            exports.shownotifi('操作失败！' + m.msg, 'error');
            return false;
        }
        if (m.attention == true) {
            exports.shownotifi('警告！' + m.msg, 'attention');
            return true;
        }
        exports.clearnotifi();
        return true;
    }

});